package com.imooc.controller;

import com.imooc.service.EsService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("items")
public class ItemsController {

    @Autowired
    private EsService esService;


    @ApiOperation(value = "搜索商品列表", httpMethod = "GET")
    @GetMapping("es/search")
    public IMOOCJSONResult search(
            String keywords,
            String sort,
            Integer page,
            Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg("搜索关键词不能为空");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }

        page--;

        PagedGridResult result = esService
                .searchItems(keywords, sort, page, pageSize);
        return IMOOCJSONResult.ok(result);
    }

}
